﻿using GZCH.Business;
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Web.FireAdmin.Phone.Controlers
{
    /// <summary>
    /// LoginAshx 的摘要说明
    /// </summary>
    public class LoginAshx : IHttpHandler, IRequiresSessionState
    {
        private HttpContext context;

        public void ProcessRequest(HttpContext context1)
        {
            context = context1;
            Hashtable hash = new Hashtable();
            try
            {
                if (context.Session["ranCode"] == null)
                {
                    throw new Exception("验证码获取错误,请重试！");
                }
                string randomCode = context.Session["ranCode"].ToString().Trim().ToLower();  //获取验证码,并转为小写
                string enterCode = context.Request.Form["yzCode"].ToString().Trim().ToLower();
                if (!randomCode.Equals(enterCode))  //验证码校对
                {
                    throw new Exception("验证码不正确！");
                }

                string txUserName = context.Request.Form["account"].ToString();
                string password = context.Request.Form["password"].ToString();

                if (new frmUserBLL().UserLogin(txUserName.Trim(), password.Trim()))
                {                
                    hash["code"] = 0;
                    hash["message"] = "success";
                }
                else
                {
                    throw new Exception("用户名不存在,或密码有误！");
                }
            }
            catch (Exception e)
            {
                hash["code"] = 1;
                hash["message"] = e.Message;
            }

            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            context.Response.Write(JsonMapper.ToJson(hash));
            context.Response.End();
        }


        private void showError(string message, string code = "1")
        {
            Hashtable hash = new Hashtable();
            hash["code"] = code;
            hash["message"] = message;
            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            context.Response.Write(JsonMapper.ToJson(hash));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}